package com.hit.server.biz.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hit.common.core.util.HitConvertUtils;
import com.hit.common.core.util.R;
import com.hit.common.log.annotation.SysLog;
import com.hit.server.api.entity.RobotClientEntity;
import com.hit.server.api.dto.RobotClientQueryDTO;
import com.hit.server.api.dto.RobotClientSaveDTO;
import com.hit.server.api.dto.RobotClientUpdateDTO;
import com.hit.server.api.vo.RobotClientVO;
import com.hit.server.api.service.RobotClientService;
import com.hit.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 执行端
 *
 * @author kehanjiang
 * @date 2025-04-03 17:25:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/robotClient" )
@Tag(description = "robotClient" , name = "执行端管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RobotClientController {

    private final  RobotClientService robotClientService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dto 执行端
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R<Page<RobotClientVO>> getRobotClientPage(@ParameterObject Page page, @ParameterObject RobotClientQueryDTO dto) {
	    dto = HitConvertUtils.emptyToNull(dto);
        return R.ok(robotClientService.getRobotClientPage(page, dto));
    }


    /**
     * 通过id查询执行端
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    public R<RobotClientVO> getById(@PathVariable("id" ) Long id) {
        return R.ok(robotClientService.getRobotClientById(id));
    }

    /**
     * 新增执行端
     * @param dto 执行端
     * @return R
     */
    @Operation(summary = "新增执行端" , description = "新增执行端" )
    @SysLog("新增执行端" )
    @PostMapping
    public R<Boolean> save(@RequestBody RobotClientSaveDTO dto) {
        return R.ok(robotClientService.saveRobotClient(dto));
    }

    /**
     * 修改执行端
     * @param dto 执行端
     * @return R
     */
    @Operation(summary = "修改执行端" , description = "修改执行端" )
    @SysLog("修改执行端" )
    @PutMapping
    public R<Boolean> updateById(@RequestBody RobotClientUpdateDTO dto) {
        return R.ok(robotClientService.updateRobotClientById(dto));
    }

    /**
     * 通过id删除执行端
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除执行端" , description = "通过id删除执行端" )
    @SysLog("通过id删除执行端" )
    @DeleteMapping
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(robotClientService.removeRobotClientBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param robotClient 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<RobotClientEntity> export(RobotClientEntity robotClient,@RequestParam List<Long> ids) {
        return robotClientService.list(Wrappers.lambdaQuery(robotClient).in(ArrayUtil.isNotEmpty(ids), RobotClientEntity::getId, ids));
    }
}